/**
 * 根据起止日期计算年龄，当参数不合法，返回 -1
 * @param {number[]} birthDate 出生日期，格式[年, 月, 日]
 * @param {number[]} currentDate 当前日期，格式[年, 月, 日]
 * @returns {number}
 */
export default function calculateAge(birthDate, currentDate) {
    if (!birthDate || !currentDate) return -1;
    if (!checkValid(birthDate) || !checkValid(currentDate)) return -1;

    const [y0, m0, d0] = birthDate;
    const [y1, m1, d1] = currentDate;
    const result = y1 - y0;
    if (m1 < m0) {
        return result - 1;
    }

    if (m1 === m0 && d1 < d0) {
        return result - 1;
    }

    return result;
}

function checkValid(date) {
    if (Array.isArray(date) === false) return false;
    if (date.length < 3) return false;

    for (let i = 0; i < date.length; i++) {
        if (isNaN(date[i])) return false;
    }

    return true;
}